﻿/*
* ========================================================================
* 类说明：
*   
* 
* 作者：HGS   时间：2024/11/21 15:10:37
* 
* 修改者：         时间：           
* 修改说明：
* ========================================================================
*/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZLGCANoptimize.方法
{
    public class CommonWay
    {
        /// <summary>
        /// 字节转16进制，中间带空格
        /// </summary>
        /// <param name="byData">字节数组的值</param>
        /// <param name="isUpper">是否输出大写格式结果，默认大写</param>
        /// <returns> "" 表示传递的值为空或者长度为0，其他则为转换后的值</returns>
        public static string ByteToHex(byte[] byData, bool isUpper = true)
        {
            if (null == byData || byData.Length <= 0)
            {
                return "";
            }
            string resultStr = "";
            for (int x = 0; x < byData.Length; x++)
            {
                if (isUpper)
                {
                    resultStr += byData[x].ToString("X2").PadLeft(2, '0').PadRight(3, ' ');
                }
                else
                {
                    resultStr += byData[x].ToString("x2").PadLeft(2, '0').PadRight(3, ' ');
                }

            }

            return resultStr.Trim();
        }
        /// <summary> 
        /// 字节转16进制，中间不带空格
        /// </summary>
        /// <param name="byData">字节数组的值</param>
        /// <param name="isUpper">是否输出为大写,默认大写</param>
        /// <returns>"" 输入字节数值的值为空或者长度为0，其他则为转换后值</returns>
        public static string ByteToHexNoSpaces(byte[] byData, bool isUpper = true)
        {
            if (null == byData || byData.Length <= 0)
            {
                return "";
            }

            string resultStr = "";
            for (int x = 0; x < byData.Length; x++)
            {
                if (isUpper)
                {
                    resultStr += byData[x].ToString("X2").PadLeft(2, '0');
                }
                else
                {
                    resultStr += byData[x].ToString("x2").PadLeft(2, '0');
                }
            }
            return resultStr.Trim();
        }
        /// <summary>
        /// 16进制转换成byte
        /// </summary>
        /// <param name="hexStr">16进制值</param>
        /// <returns>null 传递值为空或非正确的16进制值，其他则成功转换</returns>
        public static byte[] HexToByte(string hexStr)
        {
            hexStr += "";
            hexStr = hexStr.Trim().Replace(" ", ""); //去掉16进制中间的空格
            if (0 == hexStr.Length || hexStr.Length % 2 != 0)
            {
                return null;
            }

            byte[] buffer = new byte[hexStr.Length / 2];
            for (int i = 0; i < hexStr.Length; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(hexStr.Substring(i, 2), 16);
            }

            return buffer;
        }
        /// <summary>
        /// 16进制转int
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static int HexToInt(string hexStr)
        {
            //将十六进制的长度值转换为十进制表示并返回
            bool success = int.TryParse(hexStr, System.Globalization.NumberStyles.HexNumber, null, out int decimalValue);
            if (success)
            {
                return decimalValue;
            }

            return -1;
        }
        /// <summary>
        /// 单个16进制内容转Char
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static string HexToChar(string hexStr)
        {
            int hexValue = Convert.ToInt32(hexStr, 16);
            char charValue = (char)hexValue;
            return charValue.ToString();
        }
        /// <summary>
        /// 16进制转ASCII码
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static string HexToASCII(string hexStr)
        {
            hexStr = hexStr.Trim().Replace(" ", ""); //去掉16进制中间的空格
            if (hexStr.Length == 0 || hexStr.Length % 2 != 0)
            {
                throw new ArgumentException("hex value length error");
            }
            byte[] buffer = new byte[hexStr.Length / 2];
            for (int i = 0; i < hexStr.Length; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(hexStr.Substring(i, 2), 16);
            }
            string ascStr = Encoding.ASCII.GetString(buffer);

            return ascStr;
        }
        /// <summary>
        /// 验证字符串是否为日期
        /// </summary>
        /// <param name="timeString"></param>
        /// <returns></returns>
        public static bool IsValidYYMMDD(string timeString, string timeFormat = "yyMMdd")
        {
            if (string.IsNullOrEmpty(timeString))
            {
                return false;
            }

            DateTime dateValue;
            return DateTime.TryParseExact(timeString, timeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue);
        }
    }
}
